const express = require('express');
var login = express.Router();
var common = require('../../lib/common');

login.get('/', function (req, res) {
    res.render('admin/public/login.ejs');
});


//校验登录
login.post('/', function (req, res, next) {
    //接收提交的post数据
    var username = req.body.username;
    var password = req.body.password;
    common.db().query('select * from admin where account = "'+username+'"' , function (err, data) {
        if (err) {
            console.log(err);
            res.status(500).send('DATABASE error').end();
        } else {
            if (data.length == 0) {
                res.status(400).send('用户名错误').end();
            } else {
                //比对密码
                if (common.md5(password) != data[0]['pass']) {
                    console.log(err);
                    res.status(400).send('密码错误').end();
                }else{
                    req.session['admin'] = data;
                    res.redirect('/admin');
                }
            }
        }
    });
});
module.exports = login;